{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "codes = pd.read_csv('cleaned_discharge_summaries.csv').dropna()\n",
    "\n",
    "def has_label(x, positive_icd9) :\n",
    "    return any([positive_icd9 in y for y in x.split(';')])\n",
    "\n",
    "codes['has_c1'] = codes['ICD9_CODE'].apply(lambda x : has_label(x, '285.1'))\n",
    "codes['has_c2'] = codes['ICD9_CODE'].apply(lambda x : has_label(x, '285.2'))\n",
    "\n",
    "codes.groupby(['has_c1', 'has_c2']).size()\n",
    "\n",
    "data_for_pos_label = codes[(codes['has_c1'] == True) & (codes['has_c2'] == False)]\n",
    "data_for_neg_label = codes[(codes['has_c2'] == True) & (codes['has_c1'] == False)]\n",
    "\n",
    "data = pd.concat([data_for_neg_label, data_for_pos_label]).reset_index(drop=True)\n",
    "data['target'] = data['has_c1'].apply(lambda x : 1 if x else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "idxs = {}\n",
    "idxs['train'], idxs['test'] = train_test_split(data.index, stratify=data['target'], test_size=0.2, random_state=12939)\n",
    "idxs['train'], idxs['dev'] = train_test_split(idxs['train'], stratify=[data['target'][i] for i in idxs['train']], \n",
    "                                              test_size=0.15, random_state=13448)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = ['train', 'dev', 'test']\n",
    "import numpy as np\n",
    "texts = {}\n",
    "labels = {}\n",
    "for k in keys :\n",
    "    filtered = data[data.index.isin(idxs[k])]\n",
    "    texts[k] = list(filtered['TEXT'])\n",
    "    labels[k] = list(filtered['target'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_texts = []\n",
    "df_labels = []\n",
    "df_exp_split = []\n",
    "\n",
    "for k in keys :\n",
    "    df_texts += texts[k]\n",
    "    df_labels += labels[k]\n",
    "    df_exp_split += [k]*len(texts[k])\n",
    "    \n",
    "df = pd.DataFrame({'text' : df_texts, 'label' : df_labels, 'exp_split' : df_exp_split}) \n",
    "df.to_csv('mimic_anemia_dataset.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vocabulary size :  16274\n",
      "Found 16270 words in model out of 16274\n"
     ]
    }
   ],
   "source": [
    "%run \"../preprocess_data_BC.py\" --data_file mimic_anemia_dataset.csv --output_file ./vec_anemia.p \\\n",
    "--word_vectors_type mimic --min_df 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
